package com.ruoyi.controller.jpjs.costsummary;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.service.SysParameterService;
import com.ruoyi.service.jpjs.costsummary.CostSummaryService;
import com.ruoyi.service.jpjs.project.ProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/costSummary")
public class CostSummaryController extends BaseController {

    @Autowired
    private CostSummaryService costSummaryService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private SysParameterService sysParameterService;


    private String prefix = "jpjs/costsummary";

    @GetMapping()
    public String list(ModelMap mmp) {
        mmp.put("year", sysParameterService.findNameByType("年份"));
        mmp.put("month", sysParameterService.findNameByType("月份"));
        return prefix + "/CostSummary";
    }

    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        return projectService.getProjectTree();
    }

    @PostMapping("/findCompId")
    @ResponseBody
    public String findCompId(String proId) {
        return projectService.findCompId(proId);
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<Map<String, Object>> list(@RequestParam Map<String, Object> map) {
        return costSummaryService.findCostSummaryAll(map);
    }

    @RequestMapping("/listMonth")
    @ResponseBody
    public List<Map<String, Object>> listMonth(@RequestParam Map<String, Object> map) {
        return costSummaryService.findCostSummaryMonthAll(map);
    }

    @GetMapping("/add/{ids}")
    public String add(@PathVariable("ids") String ids, ModelMap mmp) {
        String[] arr = ids.split(",");
        mmp.put("month", sysParameterService.findNameByType("月份"));
        mmp.put("maxDate", costSummaryService.findMaxDate(arr[0], arr[1], arr[2]));
        mmp.put("companyId", arr[0]);
        mmp.put("proId", arr[1]);
        mmp.put("buildingId", arr[2]);
        return prefix + "/add";
    }

    @RequestMapping("/addList")
    @ResponseBody
    public TableDataInfo addList(@RequestParam Map<String, Object> map) {
        return getDataTable(costSummaryService.addFindCostSummaryAll(map));
    }

    @RequestMapping("/readOnly")
    @ResponseBody
    public int readOnly(@RequestParam Map<String, Object> map) {
        return costSummaryService.readOnly(map);
    }

    @PostMapping("/save")
    @ResponseBody
    public AjaxResult saveCostSummary(@RequestBody List<Map<String, Object>> list) {
        return toAjax(costSummaryService.saveCostSummary(list));
    }

    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(@RequestParam Map<String, Object> map) {
        return toAjax(costSummaryService.remove(map));
    }

    @GetMapping("/copy")
    public String copy(@RequestParam Map<String, Object> map, ModelMap mmp) {
        mmp.put("project", map);
        mmp.put("date", costSummaryService.findDate(map));
        mmp.put("building", projectService.findBuilding(map));
        mmp.put("year", sysParameterService.findNameByType("年份"));
        mmp.put("month", sysParameterService.findNameByType("月份"));
        return prefix + "/Copy";
    }

    @PostMapping("/copy")
    @ResponseBody
    public AjaxResult copy(@RequestParam Map<String, Object> map) {
        return toAjax(costSummaryService.copy(map));
    }

    @GetMapping("/findCurrentUnivalent")
    public String findCurrentUnivalent(ModelMap mmp) {
        mmp.put("year", sysParameterService.findNameByType("年份"));
        mmp.put("month", sysParameterService.findNameByType("月份"));
        return prefix + "/currentUnivalent";
    }

    @PostMapping("/findCurrentUnivalent")
    @ResponseBody
    public TableDataInfo findCurrentUnivalent(@RequestParam Map<String, Object> map) {
        return getDataTable(costSummaryService.findCurrentUnivalent(map));
    }

    @PostMapping("/exportCurrentUnivalent")
    @ResponseBody
    public AjaxResult exportCurrentUnivalent(@RequestParam Map<String, Object> map) {
        return costSummaryService.exportCurrentUnivalent(map);
    }

    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(@RequestParam Map<String, Object> map) {
        return costSummaryService.export(map);
    }

    @PostMapping("/exportBuilding")
    @ResponseBody
    public AjaxResult exportBuilding(@RequestParam Map<String, Object> map) {
        return costSummaryService.exportBuilding(map);
    }

    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) {
        return toAjax(costSummaryService.importData(file));
    }

    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        return costSummaryService.importTemplate();
    }

}
